//
//  MCLotteryHalllPickTableViewCell.m
//  TLYL
//
//  Created by miaocai on 2017/6/2.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCLotteryHalllPickTableViewCell.h"
#import "MCFilterCollectionViewCell.h"
#import "MCBallCollectionViewCell.h"
#import "MCBallCollectionModel.h"
#import "MCStakeUntits.h"
#import "NSString+Helper.h"
#import "MCRandomUntits.h"

@interface MCLotteryHalllPickTableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource,UITextViewDelegate,UIAlertViewDelegate>

#pragma mark - property
/** 说明信息*/
@property (nonatomic,weak) UILabel *infoLabel;
/** 筛选条件*/
@property (nonatomic,strong) UICollectionView *filterCriteriaColletionView;
/** 筛选条件数据源*/
@property (nonatomic,strong) NSMutableArray *filterArray;
/** fitersize*/
@property (nonatomic,assign) CGFloat fiterWidth;
/** 球的宽高*/
@property (nonatomic,assign) CGFloat ballWidth;
/** 选中的cell*/
@property (nonatomic,strong) NSIndexPath *selectedIndex;
/** 单式view*/
@property (nonatomic,weak) UIView *mcDSView;
/** 单式textView*/
@property (nonatomic,weak) UITextView *mcTextView;
/** 单式textView*/
@property (nonatomic,weak) UIButton *delRepartBtn;
/** 单式textView*/
@property (nonatomic,weak) UIButton *delWrongBtn;
/** 单式textView*/
@property (nonatomic,weak) UILabel *infoDanShiLabel;
/** 单式textView*/
@property (nonatomic,weak)UILabel *textViewPlaceholder;
/** 单式textView 文本内容*/
@property (nonatomic,strong) NSString *numberString;
/** 球行数*/
@property (nonatomic,assign) CGFloat lineCount;
/** 是否是和值*/
@property (nonatomic,assign) BOOL sumValue;
/** 去错后数组*/
@property (nonatomic,strong) NSArray *numberDelWrongStringArr;




@end


@implementation MCLotteryHalllPickTableViewCell

#pragma mark - init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor=[UIColor clearColor];
        self.baseSlectedModel = [[MCBaseSelectedModel alloc] init];
        
        [self setUpUI];
       
        [self regesterNotification];
    }
    return self;
}

- (void)setUpUI{

    UILabel *infoLabel = [[UILabel alloc] init];
    [self addSubview:infoLabel];
    self.infoLabel = infoLabel;
    self.infoLabel.textColor = MC_ColorWithAlpha(119, 119, 119, 1);
    self.infoLabel.font = [UIFont boldSystemFontOfSize:14.0f];
    [self.filterArray removeAllObjects];
    NSArray *arr = @[@"全",@"大",@"小",@"奇",@"偶",@"清"];
    for (NSInteger i = 0; i< arr.count; i++) {
        MCfilterBallModel *model = [[MCfilterBallModel alloc] init];
        model.info = arr[i];
        model.selected = NO;
        [self.filterArray addObject:model];
    }
    CGFloat labelWidth = self.infoLabel.frame.size.width;
    CGFloat padding = 20;
    CGFloat w = (G_SCREENWIDTH - labelWidth - (self.filterArray.count ) * padding) /self.filterArray.count;
    self.fiterWidth = w;
    
    
    
    UICollectionViewFlowLayout *ballLayout = [[UICollectionViewFlowLayout alloc] init];
//    CGFloat bw = (G_SCREENWIDTH - labelWidth - 3 * padding - 5 * 15) /5;
    CGFloat bw = (G_SCREENWIDTH - 5*10 -20*2 ) /6;
    ballLayout.minimumLineSpacing = 15;
    ballLayout.minimumInteritemSpacing = 10;
    ballLayout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
    self.ballWidth = bw;
    ballLayout.itemSize = CGSizeMake(bw, bw);
    UICollectionView *ballColletionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:ballLayout];
    [ballColletionView registerNib:[UINib nibWithNibName:NSStringFromClass([MCBallCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"MCBallCollectionViewCell"];
    ballColletionView.delegate = self;
    ballColletionView.dataSource = self;
    ballColletionView.scrollEnabled = NO;
    [self addSubview:ballColletionView];
    self.ballColletionView = ballColletionView;
    
    /*
     * 筛选条件
     */
    UICollectionViewFlowLayout *filterLayout = [[UICollectionViewFlowLayout alloc] init];
    filterLayout.itemSize = CGSizeMake(w, w);
    filterLayout.minimumLineSpacing = 0;
    filterLayout.minimumInteritemSpacing = padding * 0.5;
    filterLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    UICollectionView *filterCriteriaColletionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:filterLayout];
//    filterCriteriaColletionView.layer.cornerRadius = (self.fiterWidth  + 10)* 0.5;
//    filterCriteriaColletionView.layer.masksToBounds = YES;
//    filterCriteriaColletionView.layer.borderWidth = 2.0f;
//    filterCriteriaColletionView.layer.borderColor = MC_BALLNORMAL_COLOR.CGColor;
    [filterCriteriaColletionView registerNib:[UINib nibWithNibName:NSStringFromClass([MCFilterCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"MCFilterCollectionViewCell"];
    filterCriteriaColletionView.delegate = self;
    filterCriteriaColletionView.dataSource = self;
    [self addSubview:filterCriteriaColletionView];
    self.filterCriteriaColletionView = filterCriteriaColletionView;
    self.filterCriteriaColletionView.backgroundColor = [UIColor clearColor];
    filterCriteriaColletionView.scrollEnabled = NO;
 
    
    /**单式输入框*/
    UIView *mcTextView = [[UIView alloc] init];
    [self addSubview:mcTextView];
    self.mcDSView = mcTextView;
//    self.mcDSView.backgroundColor = [UIColor whiteColor];
    UITextView *textView = [[UITextView alloc] init];
    [mcTextView addSubview:textView];
//    textView.layer.cornerRadius = 6.0;
    textView.layer.borderWidth = 1.0;
    textView.layer.borderColor = RGB(200, 200, 200).CGColor;
    self.mcTextView = textView;
    textView.delegate = self;
    UILabel *textViewPlaceholder = [[UILabel alloc] init];
    textViewPlaceholder.attributedText = [[NSAttributedString alloc] initWithString:@"请输入号码球内容" attributes:@{NSForegroundColorAttributeName : RGB(130, 130, 130),NSFontAttributeName : [UIFont systemFontOfSize:12]}];
    [textView addSubview:textViewPlaceholder];
    self.textViewPlaceholder = textViewPlaceholder;
    
    
    /*
     * 去重复号
     */
    UIButton *quCFBtn = [[UIButton alloc] init];
    quCFBtn.titleLabel.font    = [UIFont systemFontOfSize: 15];
    [mcTextView addSubview:quCFBtn];
    quCFBtn.backgroundColor = RGB(211, 215, 214);
    [quCFBtn setTitle:@"去重复号" forState:UIControlStateNormal];
    [quCFBtn setTitleColor:RGB(50, 50, 50) forState:UIControlStateNormal];
    self.delRepartBtn = quCFBtn;
    
    
    /*
     * 去错误号
     */
    UIButton *quCWBtn = [[UIButton alloc] init];
    [mcTextView addSubview:quCWBtn];
    quCWBtn.titleLabel.font    = [UIFont systemFontOfSize: 15];
    quCWBtn.backgroundColor = RGB(211, 215, 214);
    [quCWBtn setTitle:@"去错误号" forState:UIControlStateNormal];
    [quCWBtn setTitleColor:RGB(50, 50, 50) forState:UIControlStateNormal];
    self.delWrongBtn = quCWBtn;
    
    
    /*
     * 格式说明
     */
    UILabel *infoDanShiLabel = [[UILabel alloc] init];
    infoDanShiLabel.font = [UIFont systemFontOfSize:12];
    infoDanShiLabel.textColor = [UIColor darkGrayColor];
    [mcTextView addSubview:infoDanShiLabel];
    textView.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    self.infoDanShiLabel = infoDanShiLabel;
    self.infoDanShiLabel.numberOfLines = 0;
    infoDanShiLabel.text = @"这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息这是说明信息";
    [quCWBtn addTarget:self action:@selector(delWrongNumber) forControlEvents:UIControlEventTouchDown];
    
    [quCFBtn addTarget:self action:@selector(delRepeatNumber) forControlEvents:UIControlEventTouchDown];
    self.infoLabel.textAlignment = NSTextAlignmentLeft;
    self.ballColletionView.backgroundColor = [UIColor clearColor];
    
//    self.infoLabel.layer.borderWidth = 1.0;
//    self.infoLabel.layer.borderColor = [UIColor redColor].CGColor;
//    self.ballColletionView.layer.borderWidth = 1.0;
//    self.ballColletionView.layer.borderColor = [UIColor blackColor].CGColor;
//    self.filterCriteriaColletionView.layer.borderColor = [UIColor blueColor].CGColor;
//    self.filterCriteriaColletionView.layer.borderWidth = 1.00f;
}



#pragma mark - layout
- (void)layoutSubviews{
    
    [super layoutSubviews];
 #warning  FIXME !!! 修改数字
    CGFloat padding = MC_REALVALUE(20);
    CGFloat h = self.ballArray.count / self.lineCount;

    [self.infoLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(MC_REALVALUE(10));
        make.top.equalTo(self.mas_top).offset(MC_REALVALUE(10));
        make.right.equalTo(self.mas_right).offset(-MC_REALVALUE(10));
        make.height.equalTo(@(self.fiterWidth));

    }];
   
    if ([self.baseWFmodel.filterCriteria  isEqualToString: @"0"]) {

        self.ballColletionView.hidden = NO;
        self.filterCriteriaColletionView.hidden = YES;
        self.mcDSView.hidden = YES;
        self.infoLabel.hidden = NO;
        [self.ballColletionView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.mas_left).offset(0);
            make.right.equalTo(self).offset(0);
            make.top.equalTo(self.infoLabel.mas_bottom).offset(padding);
            make.height.equalTo(@(ceilf(h) * self.ballWidth + MC_REALVALUE(10) + h * MC_REALVALUE(15)));
            
//            make.left.equalTo(self.mas_left).offset(2 *padding);
//            make.right.equalTo(self).offset(-padding);
//            make.top.equalTo(self.infoLabel.mas_bottom).offset(padding);
//            make.height.equalTo(@(ceilf(h) * self.ballWidth + MC_REALVALUE(10) + h * MC_REALVALUE(15)));
            
        }];
    } else if ([self.baseWFmodel.filterCriteria  isEqualToString: @"1"]) {
        self.ballColletionView.hidden = NO;
        self.filterCriteriaColletionView.hidden = NO;
        self.mcDSView.hidden = YES;
        self.infoLabel.hidden = NO;
        [self.filterCriteriaColletionView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.infoLabel.mas_bottom).offset(padding * 0.5);
            make.left.equalTo(self.mas_left).offset(padding);
            make.right.equalTo(@(- padding));
            make.height.equalTo(@(self.fiterWidth + MC_REALVALUE(10)));
        }];
        

        [self.ballColletionView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.mas_left).offset(0);
            make.right.equalTo(self).offset(0);
            make.top.equalTo(self.filterCriteriaColletionView.mas_bottom).offset(padding);
            make.height.equalTo(@(ceilf(h) * self.ballWidth + MC_REALVALUE(10) + h * MC_REALVALUE(15)));
            
            
//            make.left.equalTo(self.mas_left).offset(2 * padding);
//            make.right.equalTo(self.filterCriteriaColletionView.mas_right).offset(-padding);
//            make.top.equalTo(self.filterCriteriaColletionView.mas_bottom).offset(padding);
//            make.height.equalTo(@(ceilf(h) * self.ballWidth + MC_REALVALUE(10) + h * MC_REALVALUE(15)));

            
        }];
    }else{
        self.ballColletionView.hidden = YES;
        self.filterCriteriaColletionView.hidden = YES;
        self.mcDSView.hidden = NO;
        self.infoLabel.hidden = YES;
        [self.mcDSView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        [self.mcTextView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mcDSView).offset(padding * 0.5);
            make.top.equalTo(self.mcDSView).offset(padding * 0.5);
            make.right.equalTo(self.mcDSView).offset(-padding * 0.5);
            make.height.equalTo(@(G_SCREENWIDTH - 2*padding - MC_REALVALUE(180)));
        }];
        [self.delRepartBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mcDSView).offset(padding * 0.5);
            make.top.equalTo(self.mcTextView.mas_bottom).offset(0);
            make.width.equalTo(@(MC_REALVALUE(90)));
            make.height.equalTo(@(MC_REALVALUE(44)));
        }];
        [self.delWrongBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.delRepartBtn);
            make.right.equalTo(self.mcDSView).offset(-padding * 0.5);
            make.width.equalTo(@(MC_REALVALUE(90)));
            make.height.equalTo(@(MC_REALVALUE(44)));
            
        }];
        
        [self.infoDanShiLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.delRepartBtn.mas_bottom).offset(5);
            make.left.equalTo(self.mcDSView).offset(padding * 0.5);
            make.right.equalTo(self.mcDSView).offset(-padding * 0.5);
        }];
        
        [self.textViewPlaceholder mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.left.equalTo(self.mcTextView).offset(MC_REALVALUE(8));
        }];
    }

}



#pragma mark -collectionView datasource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (collectionView == self.filterCriteriaColletionView) {
        return self.filterArray.count;
    } else {
        return self.ballArray.count;
    }

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = nil;
    
    if (collectionView == self.filterCriteriaColletionView) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MCFilterCollectionViewCell" forIndexPath:indexPath];
        MCFilterCollectionViewCell *cellTemp = (MCFilterCollectionViewCell *)cell;
        cellTemp.dataSource = self.filterArray[indexPath.row];
        
        
    } else {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MCBallCollectionViewCell" forIndexPath:indexPath];
        MCBallCollectionViewCell *cellTemp = (MCBallCollectionViewCell *)cell;
        MCBallCollectionModel *model = self.ballArray[indexPath.row];
        __weak MCBallCollectionViewCell *cellTempWeak = cellTemp;
        __weak typeof(self)weakSelf = self;
        cellTemp.selectedBallK3TongXuan = ^(MCBallCollectionModel *model){
            if ([weakSelf.baseWFmodel.tongxuan isEqualToString:@"1"]) {
                if (model.seleted == YES) {
                    for (NSInteger i = 0; i<self.ballArray.count; i++) {
                        MCBallCollectionModel *model = weakSelf.ballArray[i];
                        model.seleted = YES;
                        if (model.seleted == YES && ![weakSelf.selectedBallArray containsObject:model.textInfo]) {
                            [self.selectedBallArray addObject:model.textInfo];
                        }
                    }
                }else{
                    for (NSInteger i = 0; i<weakSelf.ballArray.count; i++) {
                        MCBallCollectionModel *model = weakSelf.ballArray[i];
                        model.seleted = NO;
                        [weakSelf.selectedBallArray removeAllObjects];
                    }
                }
                [weakSelf.ballColletionView reloadData];
                if (weakSelf.slectedBallBlock != nil) {
                    weakSelf.slectedBallBlock(weakSelf.selectedBallArray);
                }
                return;
            }
        };
        cellTemp.selectedBall = ^(MCBallCollectionModel *model){
            if ([model.mutex isEqualToString:@"0"]) {// 不互斥
                  [weakSelf addSelectedBallToArray:model];
            }
            else if ([model.mutex isEqualToString:@"1"]){// 互斥
                        if (model.seleted == YES && ![weakSelf.selectedBallArray containsObject:model.textInfo]) {
                            if ([weakSelf.dataSource.maxBallNumber intValue] > weakSelf.selectedBallArray.count) {
                                     [weakSelf.selectedBallArray addObject:model.textInfo];
                            } else {
                                [MBProgressHUD showError:weakSelf.dataSource.info];
                                 cellTempWeak.iconBtn.selected = NO;
                                model.seleted = NO;
                                return;
                            }
                        } else if(model.seleted == NO && [self.selectedBallArray containsObject:model.textInfo]){
                            [weakSelf.selectedBallArray removeObject:model.textInfo];
                            
                        }else{
              
                            
                        
                        }
            }else if ([model.mutex isEqualToString:@"2"]){
                if(cellTemp.ballItemSelected == YES){
                    [weakSelf.selectedBallArray removeAllObjects];
                    [weakSelf.selectedBallArray addObject:model.textInfo];
                }
            }else{
            
            
            }
            if (weakSelf.slectedBallBlock != nil) {
                weakSelf.slectedBallBlock(weakSelf.selectedBallArray);
            }
        };
        cellTemp.dataSource = model;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HEY_BOARD_HIDEN" object:nil userInfo:nil];
    if (collectionView == self.filterCriteriaColletionView) {
        [self filterBtnClickWithIndex:indexPath];
    } else {
        
    }
    
    
}


#pragma mark - uitextView Delegate


- (void)textViewDidChange:(UITextView *)textView{
    if ([textView.text isEqualToString:@""]) {
        
        self.textViewPlaceholder.hidden = NO;
        
    } else {
        
        self.textViewPlaceholder.hidden = YES;
    }
    
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MCSTAKEUNITS_HIDEN" object:nil];
    return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    //校验格式
    self.mcTextView.text = [NSString GetFormatStr:self.mcTextView.text WithModel:self.baseWFmodel];
    
    self.numberString = self.mcTextView.text;
    
    MCArryModel*AModel=[self.numberString MC_delWrongLottoryNumberWithWF:self.baseWFmodel andShow:NO];

    if (AModel.arr_Wrong.count<1&&AModel.arr_Result.count>0) {
        self.baseWFmodel.danShiArray = AModel.arr_Result;
        MCBallPropertyModel *model =  [MCStakeUntits GetBallPropertyWithWFModel:self.baseWFmodel];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"MCSTAKEUNITS_GETBALL_WF" object:nil userInfo:@{@"danshiModel":model}];
    }


}
#pragma mark - custom Method
- (void)getKl8RandomNumber:(NSString *)typeName{
        NSMutableArray *arr = MCRandomKl8(self.baseWFmodel, typeName);
        if (arr.count>=1) {
            NSArray *arrM = arr[0];
            for (NSString *str in arrM) {
                for (MCBallCollectionModel *model in self.ballArray) {
                    if ([model.textInfo isEqualToString:str]) {
                        model.seleted = YES;
                        
                    }
                }
            }
        }
}
- (void)filterBtnClickWithIndex:(NSIndexPath *)index{
   
    for (MCBallCollectionModel *model in self.ballArray) {
        model.seleted = NO;
        model.filterSelectd = YES;
    }
    switch (index.row) {
         
        case 0://全
            if ([self.baseWFmodel.filterCriteriaNotAll isEqualToString:@"1"]) {
                [self getKl8RandomNumber:@"全"];
                break;
            } else {
                for (MCBallCollectionModel *model in self.ballArray) {
                    model.seleted = YES;
                }
                break;
            }
        case 1://大
            if ([self.baseWFmodel.filterCriteriaNotAll isEqualToString:@"1"]) {
                [self getKl8RandomNumber:@"上"];
                break;
            } else {
            if (![self.baseWFmodel.filterCriteriaNotAll isEqualToString:@""] && self.baseWFmodel.filterCriteriaNotAll != nil) {
                for (NSInteger i = 0; i<self.ballArray.count/2; i++) {
                    MCBallCollectionModel *model = self.ballArray[i];
                    model.seleted = YES;
                }
               
            } else {
                for (NSInteger i = self.ballArray.count /2; i<self.ballArray.count; i++) {
                    MCBallCollectionModel *model = self.ballArray[i];
                    model.seleted = YES;
                }
                
            }
          
            break;
            }
        case 2://小
            
            if ([self.baseWFmodel.filterCriteriaNotAll isEqualToString:@"1"]) {
                [self getKl8RandomNumber:@"下"];
                break;
            } else {
            if (![self.baseWFmodel.filterCriteriaNotAll isEqualToString:@""]&& self.baseWFmodel.filterCriteriaNotAll != nil) {
               
                for (NSInteger i = self.ballArray.count /2; i<self.ballArray.count; i++) {
                    MCBallCollectionModel *model = self.ballArray[i];
                    model.seleted = YES;
                }
            } else {
                for (NSInteger i = 0; i<self.ballArray.count/2; i++) {
                    MCBallCollectionModel *model = self.ballArray[i];
                    model.seleted = YES;
                }
               
            }

            break;
            }
        case 3://奇数
            
            if ([self.baseWFmodel.filterCriteriaNotAll isEqualToString:@"1"]) {
                [self getKl8RandomNumber:@"奇"];
                break;
            } else {
            if ([self.baseWFmodel.ballStartNumber integerValue] == 0) {
                for (NSInteger i = 0; i<self.ballArray.count; i++) {
                    if (i%2!=0) {
                        MCBallCollectionModel *model = self.ballArray[i];
                        model.seleted = YES;
                    }
                }
            } else {
                for (NSInteger i = 1; i<=self.ballArray.count; i++) {
                    if (i%2!=0) {
                        MCBallCollectionModel *model = self.ballArray[i-1];
                        model.seleted = YES;
                    }
                }
            }
            break;
            }
        case 4://偶数
            if ([self.baseWFmodel.filterCriteriaNotAll isEqualToString:@"1"]) {
                [self getKl8RandomNumber:@"偶"];
                break;
            } else {
            if ([self.baseWFmodel.ballStartNumber integerValue] == 0) {
                for (NSInteger i = 0; i<self.ballArray.count; i++) {
                    if (i%2==0) {
                        MCBallCollectionModel *model = self.ballArray[i];
                        model.seleted = YES;
                    }
                }
            } else {
                for (NSInteger i = 1; i<=self.ballArray.count; i++) {
                    if (i%2==0) {
                        MCBallCollectionModel *model = self.ballArray[i-1];
                        model.seleted = YES;
                    }
                }
            }
            break;
            }
        case 5://清
            for (MCBallCollectionModel *model in self.ballArray) {
                
                model.seleted = NO;
            }
            
            break;
            
        default:
            break;
            
            
    }
    [self.ballColletionView reloadData];
}
- (void)addSelectedBallToArray:(MCBallCollectionModel *) model{
    
            if (model.filterSelectd == YES) {
        for (MCBallCollectionModel *model in self.ballArray) {
            if (model.seleted == YES && ![self.selectedBallArray containsObject:model.textInfo]) {
                
                [self.selectedBallArray addObject:model.textInfo];
            } if(model.seleted == NO && [self.selectedBallArray containsObject:model.textInfo]){
                [self.selectedBallArray removeObject:model.textInfo];
            }
            
        }
    } else {

            if (model.seleted == YES && ![self.selectedBallArray containsObject:model.textInfo]) {
                [self.selectedBallArray addObject:model.textInfo];
              
            } if(model.seleted == NO && [self.selectedBallArray containsObject:model.textInfo]){
                [self.selectedBallArray removeObject:model.textInfo];
            }

    }

}

- (void)regesterNotification{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLotteryWFERWF:) name:@"CHANGE_LOTTERY_WF_ERJIWF" object:nil];

   
}

/*
 * 清空输入框
 */
-(void)danShiClear{
    self.mcTextView.text=@"";
}
- (void)changeLotteryWFERWF:(NSNotification *)noti{
    [self.ballArray removeAllObjects];
    [self.ballColletionView reloadData];
    for (MCfilterBallModel *model in self.filterArray) {
        model.selected = NO;
    }
    [self.filterCriteriaColletionView reloadData];
    
    //清空输入框
    self.mcTextView.text=@"";

}
#pragma mark - touch event
#pragma mark-点击去错处理
- (void)delWrongNumber{

    //校验格式
    self.mcTextView.text = [NSString GetFormatStr:self.mcTextView.text WithModel:self.baseWFmodel];
    //去错数组
    
    MCArryModel*AModel= [self.mcTextView.text MC_delWrongLottoryNumberWithWF:self.baseWFmodel andShow:YES];
    self.numberDelWrongStringArr =AModel.arr_Result;
    if (AModel.arr_Wrong.count<1) {
    }else{
        NSString * tip=[NSString stringWithFormat:@"错误的号码,已经帮您过滤:%@",[AModel.arr_Wrong componentsJoinedByString:@","]];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:tip delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        alert.delegate=self;
        alert.alertViewStyle = UIAlertViewStyleDefault;
        [alert show];
    }
    if ([self.baseWFmodel.lotteryCategories isEqualToString:@"esf"]||[self.baseWFmodel.lotteryCategories isEqualToString:@"pks"]) {
        self.mcTextView.text = [self.numberDelWrongStringArr componentsJoinedByString:@","];
    }else{
        self.mcTextView.text = [self.numberDelWrongStringArr componentsJoinedByString:@" "];
    }

    self.baseWFmodel.danShiArray = AModel.arr_Result;
    if (AModel.arr_Result.count>0) {
        self.baseWFmodel.danShiArray = self.numberDelWrongStringArr;
        MCBallPropertyModel *model =  [MCStakeUntits GetBallPropertyWithWFModel:self.baseWFmodel];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"MCSTAKEUNITS_GETBALL_WF" object:nil userInfo:@{@"danshiModel":model}];
    }
}

#pragma mark-点击去重处理
- (void)delRepeatNumber{
    //校验格式
    self.mcTextView.text = [NSString GetFormatStr:self.mcTextView.text WithModel:self.baseWFmodel];
    
    MCArryModel *AModel=[self.mcTextView.text MC_delRepartLottoryNumberInStringmodel:self.baseWFmodel];
    
    if (AModel.arr_Repart.count>0) {
        NSString * tip=[NSString stringWithFormat:@"重复的号码,已经帮您过滤:%@",[AModel.arr_Repart componentsJoinedByString:@","]];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:tip delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        alert.delegate=self;
        alert.alertViewStyle = UIAlertViewStyleDefault;
        [alert show];
    }
    

    if ([self.baseWFmodel.lotteryCategories isEqualToString:@"esf"]||[self.baseWFmodel.lotteryCategories isEqualToString:@"pks"]) {
        self.mcTextView.text = [AModel.arr_Result componentsJoinedByString:@","];
    }else{
        self.mcTextView.text = [AModel.arr_Result componentsJoinedByString:@" "];
    }
    self.numberString = self.mcTextView.text;
    
    if (AModel.arr_Wrong.count>0) {
        
    }else{
        if (AModel.arr_Result.count>0) {
            self.baseWFmodel.danShiArray = AModel.arr_Result;
            MCBallPropertyModel *model =  [MCStakeUntits GetBallPropertyWithWFModel:self.baseWFmodel];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"MCSTAKEUNITS_GETBALL_WF" object:nil userInfo:@{@"danshiModel":model}];
        }
    }

}
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated{
    
}
/*
 * 单式  添加号码 调用检测 
 */
-(BOOL)danShiaddNumber{

    //获取切割数组
    NSArray *arr0 = [NSString splitStringByDiffrentSplitMarkInString:self.mcTextView.text andModel:self.baseWFmodel];
    //获取去错数组
    MCArryModel*AModel= [self.mcTextView.text MC_delWrongLottoryNumberWithWF:self.baseWFmodel andShow:NO];
    
    if (arr0.count<1||arr0==nil) {
        [MBProgressHUD showError:@"请至少选择1注！"];
        return NO;
    }
    //防止用户输入全部都是空格
    if (arr0.count==1&&[NSString isEmpty:self.mcTextView.text]) {
        [MBProgressHUD showError:@"请至少选择1注！"];
        return NO;
    }
    if (AModel.arr_Wrong.count>0) {
        [MBProgressHUD showError:@"请先去除错误！"];
        return NO;
    }

    self.baseWFmodel.danShiArray = AModel.arr_Result;
    return YES;

}


#pragma mark - getter and setter

/** 暂未使用*/
- (void)setLotteriesType:(NSString *)lotteriesType{
    
    
    _lotteriesType = lotteriesType;
    

    NSString *str = [MCLotteryID getLotteryCategoriesTypeNameByID:lotteriesType];
    if ([str isEqualToString:@"ssc"]) {
       self.lineCount = 5;
   
    } else if ([str isEqualToString:@"esf"]){
        self.lineCount = 5;

    }else{
        self.lineCount = 5;
    }
   
    
}
/** 取消高亮*/
- (void)setHighlighted:(BOOL)highlighted{}
/** 取消选中事件*/
- (void)setSelected:(BOOL)selected{}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{}

- (void)setRandomNumber:(NSNumber *)randomNumber{
    _randomNumber = randomNumber;
    for (NSInteger i = 0; i<self.ballArray.count; i++) {
        if ([self.randomNumber intValue] - 1 == i) {
            MCBallCollectionModel *model = self.ballArray[i];
                model.seleted = YES;
            
        }
    }
   [self.ballColletionView reloadData];

}

- (void)setBallCellTextInfo:(MCBasePWFModel *)baseWFmodel{
    if (![baseWFmodel.isAddZero isEqualToString:@"1"]) {
        for (NSInteger i = [baseWFmodel.ballStartNumber intValue]; i<= [baseWFmodel.ballEndNumber intValue]; i++) {
            NSString *str = [NSString stringWithFormat:@"%ld",(long)i];
            MCBallCollectionModel *model = [[MCBallCollectionModel alloc] init];
            model.textInfo = str;
            model.mutex = baseWFmodel.mutex;
            [self.ballArray addObject:model];
        }
    } else {
        for (NSInteger i = [baseWFmodel.ballStartNumber intValue]; i<= [baseWFmodel.ballEndNumber intValue]; i++) {
            NSString *str = [NSString stringWithFormat:@"%.2ld",(long)i];
            MCBallCollectionModel *model = [[MCBallCollectionModel alloc] init];
            model.textInfo = str;
            model.mutex = baseWFmodel.mutex;
            [self.ballArray addObject:model];
        }
    }
   
}
- (void)setBallCellTextInfoWithBallText:(MCBasePWFModel *)baseWFmodel{
    NSArray *arrStr = [baseWFmodel.ballText componentsSeparatedByString:@"、"];
    for (NSString *title in arrStr) {
        MCBallCollectionModel *model = [[MCBallCollectionModel alloc] init];
        model.textInfo = title;
        model.mutex = self.baseWFmodel.mutex;
        [self.ballArray addObject:model];
        
    }
}

- (void)setBaseWFmodel:(MCBasePWFModel *)baseWFmodel{
    _baseWFmodel = baseWFmodel;
    if (self.ballArray.count != 0) {
        return;
    }
    if ([baseWFmodel.ballText isEqualToString:@""] ) {
        [self setBallCellTextInfo:baseWFmodel];
    } else {
        if ([baseWFmodel.tongxuan isEqualToString:@"2"]) {
            if ([self.dataSource.danma isEqualToString:@"9"]) {
                [self setBallCellTextInfoWithBallText:baseWFmodel];

            } else {
                [self setBallCellTextInfo:baseWFmodel];
            }
                   }else{
                [self setBallCellTextInfoWithBallText:baseWFmodel];
        }
 
    }
#warning mark -- 优化
        [self.filterArray removeAllObjects];
        NSArray *arr = nil;
        if ([self.baseWFmodel.lotteryCategories isEqualToString:@"kl8"]) {
            arr = @[@"全",@"上",@"下",@"奇",@"偶",@"清"];
        } else {
            arr = @[@"全",@"大",@"小",@"奇",@"偶",@"清"];
        }
        for (NSInteger i = 0; i< arr.count; i++) {
            MCfilterBallModel *model = [[MCfilterBallModel alloc] init];
            model.info = arr[i];
            model.selected = NO;
            [self.filterArray addObject:model];
        }
    self.infoDanShiLabel.text = baseWFmodel.info;
}

- (void)setDataSource:(MCItemModel *)dataSource{
    
    _dataSource = dataSource;
    self.infoLabel.text = dataSource.info;
   }

- (NSMutableArray *)selectedBallArray{
    
    if (_selectedBallArray == nil) {
        _selectedBallArray = [NSMutableArray array];
    }
    return _selectedBallArray;
}

- (NSMutableArray *)filterArray{
    
    if (_filterArray == nil) {
        _filterArray = [NSMutableArray array];
        
    }
    return _filterArray;
}

- (NSMutableArray *)ballArray{
    
    if (_ballArray == nil) {
        _ballArray = [NSMutableArray array];
    }
    
    return _ballArray;
}

- (void)dealloc{
    
    NSLog(@"MCLotteryHalllPickTableViewCell -- dealloc");
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
